package com.example.kafka.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;

/**
 * @author LiYuhang
 * @version 0.1
 * @application
 * @Date 2021/6/24 19:04
 * @Description TODO
 */
@Component
@Slf4j
public class ConsumerListener {
    @KafkaListener(topics = "test",groupId = "topic.group3")
    public void topic_test(ConsumerRecord<?, ?> record,Acknowledgment ack,
                           @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {

        if (Objects.nonNull(record.value())) {
            Object msg = record.value();
            log.info("sendDate:{} ,msg:{},record:{}",getMsgSendDate(record.timestamp()),msg,record);
            ack.acknowledge();
        }
    }

    private LocalDateTime getMsgSendDate(long timestamp) {
        return LocalDateTime.ofEpochSecond(timestamp / 1000 ,0,ZoneOffset.ofHours(8));
    }

}
